import FYResMgr from "../Frame/Res/FYResMgr";
import { FYE } from "../Global/FYE";
import FYLog from "../Frame/FYLog";
import FYPlatformMgr from "./FYPlatformMgr";
import FYSDKConfig, { FYShareType, ShareResultType, VideoIDType, BannerType } from './FYSDKConfig';
import TTSDK from "./TTSDK";
/**
 * SDK管理器
 */
export default class SDKMgr {
    public static readonly Instance: SDKMgr = new SDKMgr();
    private constructor() { }

    private static readonly CustomSDKType: FYE.CustomSDKType = cc.sys.platform === cc.sys.WECHAT_GAME ? FYE.CustomSDKType.WX : cc.sys.platform === (cc.sys.MOBILE_BROWSER || cc.sys.DESKTOP_BROWSER) ? FYE.CustomSDKType.CocosCreator : FYE.CustomSDKType.CocosCreator;

    /** 振动开关 */
    public static shakeSwitch = true;

    /** 激励视频广告回调列表 */
    private arrayRewardVideoADCB: Array<Function> = [];
    /** banner字典 */
    private dictBanner: { [key: string]: wx.banner } = {};

    /**
     * Native调用JS方法
     * @param method JS方法名
     * @param value 方法值
     */
    public static NativeCallJS(method: string, value: string) {
        FYLog.log("---> NativeCallJS " + method + " " + value);
        switch (method) {
            case "showRewardVideoADSuccess": {
                SDKMgr.Instance.showRewardVideoADSuccess();
                break;
            }
            default: {
                // cc.warn("未处理的method" + method);
                return;
            }
        }
    }

    /** 显示激励视频广告 */
    public showRewardVideoAD(cb: (isEnded: boolean) => void, adType: VideoIDType) {
        if (FYPlatformMgr.isTT()) {
            TTSDK.Instance.showAd(cb, FYSDKConfig.arrayVideoID[adType]);
        } else {
            if (cb) {
                cb(true);
            }
        }
    }

    /** 显示激励视频广告 成功 */
    public showRewardVideoADSuccess() {
        if (FYPlatformMgr.isApp()) {
            if (this.arrayRewardVideoADCB[this.arrayRewardVideoADCB.length - 1]) {
                this.arrayRewardVideoADCB[this.arrayRewardVideoADCB.length - 1](true);
                this.arrayRewardVideoADCB.pop();
            }
        }
        FYLog.log("---> showRewardVideoADSuccess");
    }

    /** 显示激励视频广告 失败 */
    public showRewardVideoADFail() {
        if (FYPlatformMgr.isApp()) {
            if (this.arrayRewardVideoADCB[this.arrayRewardVideoADCB.length - 1]) {
                this.arrayRewardVideoADCB[this.arrayRewardVideoADCB.length - 1](false);
                this.arrayRewardVideoADCB.pop();
            }
        }
        FYLog.log("---> showRewardVideoADFail");
    }

    /**
     * 显示banner
     * @param bannerType banner类型
     */
    public showBanner(bannerType: BannerType | string) {
        if (FYPlatformMgr.isTT()) {
            if (typeof bannerType === "string") {
                TTSDK.Instance.showBannerAd(bannerType);
            } else {
                TTSDK.Instance.showBannerAd(FYSDKConfig.arrayBannerID[bannerType]);
            }
        }
    }

    /**
     * 隐藏banner
     * @param bannerType banner类型
     */
    public hideBanner(bannerType: BannerType | string) {
        if (FYPlatformMgr.isTT()) {
            if (typeof bannerType === "string") {
                TTSDK.Instance.hideBannerId(bannerType);
            } else {
                TTSDK.Instance.hideBannerId(FYSDKConfig.arrayBannerID[bannerType]);
            }
        }
    }

    /**
     * 分享
     * @param shareType 分享类型
     * @param cb 回调函数
     */
    public share(shareType: FYShareType, cb: (resultType: ShareResultType) => void) {
        if (FYPlatformMgr.isWX() || FYPlatformMgr.isQQGame()) {
        }
    }

    /**
     * 跳转到其他小游戏
     * @param appId 
     */
    public navigateToMiniProgram(appId) {
        if (FYPlatformMgr.isTT()) {
            if (tt.navigateToMiniProgram) {
                console.log("--> navigateToMiniProgram");
                tt.navigateToMiniProgram({ appId: appId });
            } else {
                console.log("--> navigateToMiniProgram is undefined");
                tt.showModal({
                    title: "提示",
                    content:
                        "当前客户端版本过低，无法使用该功能，请升级客户端或关闭后重启更新。"
                });
            }
        }
    }

    /**
     * 保存本地数据
     * @param key 关键字
     * @param value 值
     */
    public saveLocalData(key, value) {
        cc.sys.localStorage.setItem(key, value);
    }

    /**
     * 读取本地数据，没有返回null
     * @param key 关键字
     * @param defaultValue 默认值 当没有key对应的数据时，返回该值
     */
    public loadLocalData(key, defaultValue = "0") {
        let value = cc.sys.localStorage.getItem(key);
        if (value === 0) {
            return value
        }
        return value || defaultValue;
    }

    /**
     * 保存本地json数据
     * @param key 关键字
     * @param jsonData jsonData
     */
    public saveLocalJsonData(key, jsonData) {
        cc.sys.localStorage.setItem(key, JSON.stringify(jsonData));
    }

    /**
     * 读取本地json数据，没有返回null
     * @param key 关键字
     */
    public loadLocalJsonData(key) {
        let v = cc.sys.localStorage.getItem(key);
        let data = null;
        if (v) {
            data = JSON.parse(v);
        }
        return data;
    }

    /**
     * 移除本地数据，没有返回null
     * @param key 关键字
     */
    public removeLocalData(key) {
        cc.sys.localStorage.removeItem(key);
    }

    /**
    * 移除所有本地数据
   
    */
    public removeAllLocalData() {
        cc.sys.localStorage.clear();
    }

    /**
     * 将资源路径转成带MD5的资源路径
     * @param path 资源路径
     */
    public Md5Pipe(path) {
        let _path = path
        if (cc.loader.md5Pipe) {
            _path = cc.loader.md5Pipe.transformURL(_path);
        }
        return _path;
    }

    public getSound(type: FYE.SoundType) {
        if (!FYResMgr.Instance.dictSound[type]) {
            FYLog.log("FYResMgr.Instance.dictSound has not type is " + type + " music");
            return;
        }

        if (SDKMgr.CustomSDKType == FYE.CustomSDKType.QQPlay) {
            let audioContext = BK.createAudioContext();
            audioContext.src = "GameRes://" + FYResMgr.Instance.dictSound[type].nativeUrl;
            return audioContext;
        } else if (SDKMgr.CustomSDKType == FYE.CustomSDKType.CocosCreator) {
            let audioSource = new cc.AudioSource();
            audioSource.clip = FYResMgr.Instance.dictSound[type];
            return audioSource;
        } else if (SDKMgr.CustomSDKType == FYE.CustomSDKType.WX) {
            let InnerAudioContext = wx.createInnerAudioContext();
            let path = FYResMgr.Instance.dictSound[type].nativeUrl;
            let _path = SDKMgr.Instance.Md5Pipe(path);
            InnerAudioContext.src = _path;
            InnerAudioContext.autoplay = false;
            InnerAudioContext.loop = false;
            return InnerAudioContext;
        }
    }

    public getMusic(type: FYE.MusicType) {
        if (!FYResMgr.Instance.dictMusic[type]) {
            FYLog.error("FYResMgr.Instance.dictMusic has not type is " + type + " music");
            return;
        }
        if (SDKMgr.CustomSDKType == FYE.CustomSDKType.QQPlay) {
            let audioContext = new BK.Audio(0, "GameRes://" + FYResMgr.Instance.dictMusic[type].nativeUrl, -1);
            // let audioContext = BK.createAudioContext();
            // audioContext.src = "GameRes://" + FYResMgr.Instance.dictMusic[type].nativeUrl;
            // audioContext.loop = true;
            return audioContext;
        } else if (SDKMgr.CustomSDKType == FYE.CustomSDKType.CocosCreator) {
            let audioSource = new cc.AudioSource();
            audioSource.clip = FYResMgr.Instance.dictMusic[type];
            audioSource.loop = true;
            return audioSource;
        } else if (SDKMgr.CustomSDKType == FYE.CustomSDKType.WX) {
            let InnerAudioContext = wx.createInnerAudioContext();
            let path = FYResMgr.Instance.dictMusic[type].nativeUrl;
            let _path = SDKMgr.Instance.Md5Pipe(path);
            InnerAudioContext.src = _path;
            InnerAudioContext.autoplay = false;
            InnerAudioContext.loop = true;
            return InnerAudioContext;
        }
    }

    public playSound(audio) {
        if (SDKMgr.CustomSDKType == FYE.CustomSDKType.QQPlay) {
            let sound = audio as BK.AudioElement;
            sound.seek(0);
            sound.play();
        } else if (SDKMgr.CustomSDKType == FYE.CustomSDKType.CocosCreator) {
            let sound = audio as cc.AudioSource;
            sound.play();
        } else if (SDKMgr.CustomSDKType == FYE.CustomSDKType.WX) {
            let sound = audio as wx.InnerAudioContext;
            sound.play();
        }
    }

    public playMusic(audio) {
        if (SDKMgr.CustomSDKType == FYE.CustomSDKType.QQPlay) {
            let music = audio as BK.Audio;
            music.startMusic(null);
        } else if (SDKMgr.CustomSDKType == FYE.CustomSDKType.CocosCreator) {
            let music = audio as cc.AudioSource;
            music.play();
        } else if (SDKMgr.CustomSDKType == FYE.CustomSDKType.WX) {
            let music = audio as wx.InnerAudioContext;
            music.play();
        }
    }

    public stopMusic(audio) {
        if (SDKMgr.CustomSDKType == FYE.CustomSDKType.QQPlay) {
            let music = audio as BK.Audio;
            music.stopMusic();
        } else if (SDKMgr.CustomSDKType == FYE.CustomSDKType.CocosCreator) {
            let music = audio as cc.AudioSource;
            music.stop();
        } else if (SDKMgr.CustomSDKType == FYE.CustomSDKType.WX) {
            let music = audio as wx.InnerAudioContext;
            music.stop();
        }
    }

    public stopSound(audio) {
        if (SDKMgr.CustomSDKType == FYE.CustomSDKType.QQPlay) {
            let sound = audio as BK.AudioElement;
            sound.stop();
        } else if (SDKMgr.CustomSDKType == FYE.CustomSDKType.CocosCreator) {
            let sound = audio as cc.AudioSource;
            sound.stop();
        } else if (SDKMgr.CustomSDKType == FYE.CustomSDKType.WX) {
            let sound = audio as wx.InnerAudioContext;
            sound.stop();
        }
    }
}

// 将FYSDKMgr注册到cc里面，方便Native调用
cc["FYSDKMgr"] = SDKMgr;